package com.suneee.shop.service.impl;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suneee.base.util.ContextHelper;
import com.suneee.channel.dao.ChannelDao;
import com.suneee.channel.entity.Channel;
import com.suneee.cloud.group.dao.ShopAdminDao;
import com.suneee.cloud.group.entity.ShopAdminEntity;
import com.suneee.context.page.Page;
import com.suneee.core.error.Assert;
import com.suneee.core.error.ErrorUtils;
import com.suneee.enterprise.entity.Enterprise;
import com.suneee.resources.ProductError;
import com.suneee.shop.bean.ShopSearchBean;
import com.suneee.shop.dao.ShopDao;
import com.suneee.shop.entity.Shop;
import com.suneee.shop.service.ShopService;

@Service("api-shop")
public class ShopServiceImpl implements ShopService {

	@Autowired
	private ShopDao shopDao;
	@Autowired
	private ChannelDao channelDao;
	
	@Autowired
	private ShopAdminDao shopAdminDao;

	@Override
	@Transactional
	public Shop addShop(Shop shop) {
		Assert.notNull(shop.getName(), "店铺名称");
		Assert.notNull(shop.getIndustryId(), "行业");
		Assert.notNull(shop.getChannelCode(), "渠道");
		if (shopDao.existsName(shop.getName())) {
			throw ErrorUtils.wrap(ProductError.SHOP_NAME_DUPLICATE);
		}
		// 获取当前创建时间
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		shop.setCreateTime(timestamp);
		// 可用
		shop.setEnabled(true);
		// 创建中
		shop.setStatus(Shop.CREATE);

		Enterprise enterprise = ContextHelper.getCurrentLoginInfo().getEnterprise();
		
		//获取当前登录用户所属企业的超级管理员id
		List<Map<String,Object>> adminList = shopDao.queryForList("select admin_id from t_administrator where enterprise_code = ? and admin = 1", enterprise.getEnterpriseCode());
		String adminId = ContextHelper.getCurrentLoginInfo().getAdminId()+"";
		if (enterprise.getType().equals(Enterprise.SYSTEM)) {
			throw ErrorUtils.wrap(ProductError.ENTERPRISE_NO);
		}
		shop.setEnterpriseCode(enterprise.getEnterpriseCode());
		
		shopDao.insert(shop);
		
		Object [] args1 = new Object[2];
		args1[0] = shop.getShopId();
		args1[0] = adminList.get(0).get("admin_id");
		List<ShopAdminEntity> shopList =  shopAdminDao.selectWhere(" shop_id = ? and admin_id = ?", args1);
		
		if(null ==shopList || shopList.size() == 0)
		{
			//新增店铺的同时。 为超级管理员授权该店铺的管理权限
			shopAdminDao.inserSuperAdminfinfo(shop.getShopId() + "", adminList.get(0).get("admin_id").toString());
		}
			
			
			//若当前的登录用户不是超级管理员。 则需要赋予当前登录者的角色授权
			if(!adminId.equals(adminList.get(0).get("admin_id"))){
				Object [] obj = {shop.getShopId(),adminId};
				List<ShopAdminEntity> shopAdminList = shopAdminDao.selectWhere(" shop_id=? and admin_id=?", obj);
				if(shopAdminList == null){
					shopAdminDao.inserSuperAdminfinfo(shop.getShopId() + "", adminId);
				}
			}
		
		return shop;
	}

	@Override
	public Shop updateShop(Shop shop) {
		Assert.notNull(shop.getName(), "店铺名称");
		Assert.notNull(shop.getShopId(), "店铺ID");
		Assert.notNull(shop.getIndustryId(), "行业");
		Assert.notNull(shop.getChannelCode(), "渠道");
		Shop old = this.getShop(shop.getShopId());
		if (Shop.AUDIT.equals(old.getStatus())) {
			throw ErrorUtils.wrap(ProductError.SHOP_STATUS_DUPLICATE);
		}
		if (!old.getName().equals(shop.getName()) && shopDao.existsName(shop.getName())) {
			throw ErrorUtils.wrap(ProductError.SHOP_NAME_DUPLICATE);
		}
		old.setName(shop.getName());
		old.setRemark(shop.getRemark());
		old.setChannelCode(shop.getChannelCode());
		old.setIndustryId(shop.getIndustryId());
		shopDao.update(old);
		//shopDao.updateShop(old);
		return old;
	}

	@Override
	public void deleteShop(int shopId) {
		Assert.notNull(shopId, "店铺id");
		Shop shop = shopDao.select(shopId);
		if (Shop.CREATE.equals(shop.getStatus())) {
			shopDao.deleteByShopId(shopId);
			shopAdminDao.deleteWhere(" shop_id = ?", shopId);
		} else {
			throw ErrorUtils.wrap(ProductError.SHOP_DUPLICATE);

		}
	}

	@Override
	public Page<Map<String,Object>> findShop(ShopSearchBean bean) {
		if (bean == null) {
			bean = new ShopSearchBean();
		}
		String type = ContextHelper.getCurrentLoginInfo().getEnterprise().getType();
		String enterpriseCode = ContextHelper.getCurrentLoginInfo().getEnterprise().getEnterpriseCode();
		if (Enterprise.CLIENT.equals(type)) {
			bean.setEnterpriseCode(enterpriseCode);
		}else{
			bean.setStatus(Shop.CREATE);
		}
		Page<Map<String,Object>> findShop = shopDao.findShop(bean);
		/*	List<Map<String,Object>> items = findShop.getItems();
		if (items.size() >= 0) {
			for (int i = 0; i < items.size(); i++) {

				Shop shop = findShop.getItems().get(i);
				Channel channel = channelDao.getChannelByChannelCode(shop.getChannelCode());
				shop.setRuleJson(channel.getRuleJson());
				shop.setChannelName(channel.getName());
			}
		}*/
		return findShop;
	}

	@Override
	public Shop getShop(int shopId) {
		Assert.notNull(shopId, "店铺");
		if (shopDao.existsId(shopId)) {
			Shop shop = shopDao.select(shopId);
			Channel channel = channelDao.getChannelByChannelCode(shop.getChannelCode());
			shop.setRuleJson(channel.getRuleJson());
			shop.setChannelName(channel.getName());
			return shop;
		} else {
			throw ErrorUtils.wrap(ProductError.SHOP_DUPLICATE);
		}
	}

	@Override
	public void normalShop(int shopId) {
		Assert.notNull(shopId, "店铺id");
		Shop shop = this.getShop(shopId);
		if (!Shop.USE.equals(shop.getStatus())) {
			throw ErrorUtils.wrap(ProductError.SHOP_STATUS_NO);
		}
		shop.setEnabled(true);
		//shopDao.updateEnabled(shopId, true);
		shopDao.update(shop);
	}

	@Override
	public void invisibleShop(int shopId) {
		Assert.notNull(shopId, "店铺id");
		Shop shop = this.getShop(shopId);
		if (!Shop.USE.equals(shop.getStatus())) {
			throw ErrorUtils.wrap(ProductError.SHOP_STATUS_NO);
		}
		//shopDao.updateEnabled(shopId, false);
		shop.setEnabled(false);
		shopDao.update(shop);
	}

	@Override
	public void settingsShop(int shopId,String ruleInfo) {
		Assert.notNull(shopId, "店铺id");
		Assert.notNull(ruleInfo, "接入信息");
		Shop shop = this.getShop(shopId);
		if (Shop.AUDIT.equals(shop.getStatus())) {
			throw ErrorUtils.wrap(ProductError.SHOP_STATUS_NO);
		}
		shop.setStatus(Shop.AUDIT);
		shop.setRuleInfo(ruleInfo);
		shopDao.update(shop);
//		shopDao.updateRuleInfo(shopId, ruleInfo,shop.getStatus());
	}

	@Override
	public void updateProxyURL(int shopId, String proxyURL) {
		Assert.notNull(shopId, "店铺id");
		Assert.notNull(proxyURL, "URL配置");
		Shop shop = this.getShop(shopId);
		if (Shop.CREATE.equals(shop.getStatus())) {
			throw ErrorUtils.wrap(ProductError.SHOP_STATUS_NO);
		}
		shop.setStatus(Shop.USE);
		shop.setProxyURL(proxyURL);
		shopDao.update(shop);
		//shopDao.updateProxyURL(shopId, proxyURL,shop.getStatus());
	}

	@Override
	public List<Shop> listEnterpriseShop() {
		String enterpriseCode = ContextHelper.getCurrentLoginInfo().getEnterprise().getEnterpriseCode();
		List<Shop> shopList = shopDao.listEnterpriseShop(enterpriseCode);
		if(null != shopList && shopList.size() > 0)
		{
			for(Shop shop:shopList)
			{
				List<Map<String,Object>> objList = shopDao.queryForList("select name from t_channel where channel_code = ?", shop.getChannelCode());
				if(null != objList && objList.size() > 0)
				{
					shop.setChannelName(objList.get(0).get("name").toString());
				}
			}
		}
		return shopList;
	}

	
}
